import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../config/const_config.dart';
import 'applied/my_applied.dart';
import 'card/card.dart';
import 'introduce_page.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      // alignment: const Alignment(0, 0),
      // color: ConstConfig.cardTestColor,
      children: [
        GestureDetector(
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    fullscreenDialog: true,
                    builder: (context) => const ContractPage()));
          },
          child: Card(
            // padding: EdgeInsets.all(30.w),
            // decoration: buildBoxDecoration(),
            child: Image(
              image: const AssetImage("assets/introduce.png"),
              fit: BoxFit.fill,
              width: 1170.w,
              height: 720.h,
              // height: ScreenUtil.defaultSize.height,
            ),
          ),
        ),
        Card(
          margin: const EdgeInsets.all(10),
          // width: 1170.w,
          // height: 610.h,
          color: ConstConfig.cardColor,
          elevation: 3,
          // decoration: buildBoxDecoration(),
          // shadowColor: Colors.amber,
          child: Row(
            // 左右排列的布局 . 权重 6:5
            children: [
              Expanded(
                  flex: 8,
                  child: Column(
                    children: [
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(top: 50.h, left: 100.w),
                        child: const Text(
                          "履约概览",
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      Container(
                        alignment: Alignment.center,
                        child: Stack(
                          children: [
                            Container(
                              // color: Colors.lightBlue,
                              padding: const EdgeInsets.all(10),
                              // 修饰
                              decoration: const BoxDecoration(
                                  // color: Colors.greenAccent,
                                  color: ConstConfig.circleNoColor,
                                  // color: ConstConfig.cardTestColor,
                                  shape: BoxShape.circle,
                                  boxShadow: [
                                    BoxShadow(
                                        color: Colors.white,
                                        spreadRadius: -8,
                                        offset: Offset(-5, -5),
                                        blurRadius: 30),
                                    BoxShadow(
                                        color: Colors.blue,
                                        spreadRadius: 2,
                                        offset: Offset(7, 7),
                                        blurRadius: 20)
                                  ]),
                              child: CustomPaint(
                                size: Size(300.w, 300.w),
                                painter: CustomShapePainter(_list),
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        alignment: Alignment.center,
                        child: Text(
                          "抵扣总金额: ${getSum()}",
                        ),
                      ),
                      const Divider(
                        color: ConstConfig.cardColorNo,
                        height: 30,
                      ),
                    ],
                  )),
              Expanded(
                  flex: 5,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: _list.map((data) {
                      return Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 10, vertical: 5),
                          child: Row(children: [
                            Container(
                              color: data["color"],
                              height: 40.w,
                              width: 40.w,
                              margin: const EdgeInsets.only(right: 10),
                            ),
                            Text(
                              data["title"],
                              style: buildTextStyle(data),
                            ),
                            const SizedBox(width: 10),
                            Text(
                              data["mount"].toString(),
                              style: buildTextStyle(data),
                            )
                          ]));
                    }).toList(),
                  )),
            ],
          ),
        ),
        CardNavigator(
          title: "申请",
          iconTitle: [
            {
              "image": Image.asset(
                "assets/myReq.png",
                height: 40,
                width: 40,
              ),
              "title": "我的申请",
              "func": () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => const MyApplyPage()));
              }
            },
            {
              "image": Image.asset(
                "assets/myReq.png",
                height: 40,
                width: 40,
              ),
              "title": "减免申请",
              "func": () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => const MyStartApplyPage()));
              }
            }
          ],
        ),
        CardNavigator(
          title: "第三方链接",
          iconTitle: [
            {
              "title": "国音",
              "image": Image.asset(
                "assets/guoyin.png",
                height: 40,
                width: 40,
              ),
              "func": () {
                print("国音");
              }
            },
            {
              "image": Image.asset(
                "assets/cisan.png",
                height: 40,
                width: 40,
              ),
              "title": "慈善",
              "func": () {
                print("慈善");
              }
            }
          ],
        )
      ],
    );
  }

  TextStyle buildTextStyle(data) {
    return TextStyle(shadows: const [
      // Shadow(
      //   offset: Offset(0.0, 0.0),
      //   blurRadius: 3.0,
      //   color: Color.fromARGB(255, 0, 0, 0),
      // ),
      Shadow(
        offset: Offset(1.0, 1.0),
        blurRadius: 1.0,
        color: Color.fromARGB(125, 0, 0, 255),
      ),
    ], fontSize: 16, fontWeight: FontWeight.bold, color: data["color"]);
  }

  // final List<AmountRecord> _list = [
  //   AmountRecord("亲友", 200, const Color.fromRGBO(216, 61, 108, 1)),
  //   AmountRecord("我的", 200, const Color.fromRGBO(191, 191, 229, 1)),
  //   AmountRecord("捐赠", 200, const Color.fromRGBO(0, 206, 255, 1)),
  //   AmountRecord("其他", 200, const Color.fromRGBO(211, 173, 247, 1)),
  //   AmountRecord("其他", 200, const Color.fromRGBO(24, 144, 255, 1))
  // ];

  final List _list = [
    {
      "title": "亲友",
      "mount": 200,
      "color": const Color.fromRGBO(216, 61, 108, 1)
    },
    {
      "title": "我的",
      "mount": 100,
      "color": const Color.fromRGBO(191, 191, 229, 1)
    },
    {
      "title": "捐赠",
      "mount": 300,
      "color": const Color.fromRGBO(0, 206, 255, 1)
    },
    {
      "title": "其他",
      "mount": 200,
      "color": const Color.fromRGBO(211, 173, 247, 1)
    },
    {
      "title": "其他",
      "mount": 100,
      "color": const Color.fromRGBO(24, 144, 255, 1)
    },
  ];

  int getSum() {
    int sum = _list.map((e) => e["mount"]).reduce(
          (val, acc) => acc + val,
        );
    return sum;
  }

  BoxDecoration buildBoxDecoration() {
    return BoxDecoration(
        // color: ConstConfig.cardColor20,
        borderRadius: BorderRadius.circular(30.w),
        boxShadow: const [
          BoxShadow(
            color: ConstConfig.cardColor, // 阴影的颜色
            spreadRadius: 20, // 阴影的大小
            blurRadius: 1, // 阴影模糊度的大小，值越大阴影范围也越大，但也更透明
            offset: Offset(2, 2), // 阴影分别在水平和垂直方向上的偏移量
          ),
        ]);
  }
}

class CustomShapePainter extends CustomPainter {
  List _list;

  CustomShapePainter(this._list);

  final Paint _paint = Paint()..isAntiAlias = true;

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    // 中心点
    Offset center = Offset(size.width / 2, size.height / 2);

    //
    double radius = min(size.width / 2, size.height / 2);
    double startRadian = -pi / 2;
    double total = getSum() * 1.0;

    for (var i = 0; i < _list.length; ++i) {
      var item = _list[i];
      double flag = item["mount"] / total;
      double sweepRadian = flag * 2 * pi;

      _paint.color = item["color"];
      canvas.drawArc(Rect.fromCircle(center: center, radius: radius),
          startRadian, sweepRadian, true, _paint);
      startRadian += sweepRadian;
    }
  }

  int getSum() {
    int sum = _list.map((e) => e["mount"]).reduce(
          (val, acc) => acc + val,
        );
    return sum;
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    // 返回true 刷新
    return true;
  }
}

class Person {
  String title;
  int mount;
  Color color;

  Person(this.title, this.mount, this.color);
}
